﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using Baosight.ZNH.ZMCT.UI.ChildWindows;
using System.Windows.Controls;
using Telerik.Windows.Controls.GridView;


namespace Baosight.ZNH.ZMCT.UI
{
    public partial class ZMCT30 : EFViewCmes
    {
        public ZMCT30()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }
 
        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "短信管理-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }

            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "短信管理-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 删除按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);
        }
        /// <summary>
        /// 发送按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnSendButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;
            int count = this.grid.CheckedEIRows.Count;
            if (count <1)
            {
                this.OpenEFWindowAlert("请选择一条短信进行发送！", true);
                return;
            }
            if (this.grid.CheckedEIRows[0]["fdMobilePhone"]==null || "".Equals(this.grid.CheckedEIRows[0]["fdMobilePhone"]))
            {
                this.OpenEFWindowAlert("手机号不能为空！", true);
                return;
            }
            base.OnModifyButtonClick("send", "发送", sender, e);

        }
        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            Sms sms = new Sms(_dialogType);//构造一个新的修改页面
            sms.EIDataContext = new Platform.Core.UI.EIDataContext();
            _eIInfo.AddBlock(this.getYesOrNo("cmes.ct.yesorno", false));
            sms.EIDataContext.Update(_eIInfo);

            if (_dialogType.Equals("insert")) 
            {
                sms.fdIsTemporary.Value = "是";
                sms.choiceName.Visibility = Visibility.Collapsed;
            }
            else if (_dialogType.Equals("update")||_dialogType.Equals("detail"))
            {
                if (_eIInfo.GetBlock("result").GetRow(0)["fdIsTemporary"].Equals("是"))
                {
                    sms.choiceName.Visibility = Visibility.Collapsed;
                }
                else
                {
                    sms.fdName.Visibility = Visibility.Collapsed;
                    sms.fdMobilePhone.IsReadOnly = true;
                    sms.fdMobilePhone.Label = "手机号码";
                    sms.fdName.eBindingName = "result-0-choiceName";
                    sms.choiceName.eBindingName = "result-0-fdName";
                }
            }
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = sms;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(CmesWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口

        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
        /// <summary>
        /// 页签切换
        /// </summary>
        /// <returns></returns>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e) 
        {
            EFTab tab = sender as EFTab;
            if (tab != null)
            {
                EFTabItem tabItem = tab.SelectedItem as EFTabItem;
                if (tabItem != null && this.MessageStatus.IsSelected)
                {
                    this.MessageStatus.LoadView("ZMCT31");
                }
            }
        }
        #region 查看明细
        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;

            EIRow row =(link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "未发短信-查看", "detail", null, sender, e);
        }
        #endregion

        /// <summary>
        /// 是否选择
        /// </summary>
        /// <returns></returns>
        public EIBlock getYesOrNo(string blockName, bool isAll)
        {
            EIBlock block = new EIBlock(blockName);

            EIColumn eiColumn = new EIColumn();
            eiColumn.Name = "display";
            eiColumn.TypeLabel = "C";
            block.AddColumn(eiColumn);

            eiColumn = new EIColumn();
            eiColumn.Name = "value";
            eiColumn.SetTypeLabel("C");
            block.AddColumn(eiColumn);

            IList<EIRow> _list = new List<EIRow>();
            EIRow _map = null;

            if (isAll)
            {
                _map = new EIRow();
                _map.Add("value", "");
                _map.Add("display", "");
                _list.Add(_map);
            }

            _map = new EIRow();
            _map.Add("value", "是");
            _map.Add("display", "是");
            _list.Add(_map);

            _map = new EIRow();
            _map.Add("value", "否");
            _map.Add("display", "否");
            _list.Add(_map);

            block.AddRows(_list);
            return block;
        }
    }
}
